package structures;

import org.jgrapht.experimental.isomorphism.AdaptiveIsomorphismInspectorFactory;
import org.jgrapht.experimental.isomorphism.GraphIsomorphismInspector;
import org.jgrapht.graph.*;
import java.util.*;

public class StaticGraph {
	private DefaultDirectedWeightedGraph<String, DefaultWeightedEdge> g = new DefaultDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);

	public StaticGraph() {

	}

	public StaticGraph(StaticGraph g, Vector<String> edges) {
		for(String s : edges) {
			for(String s2 : edges) {
				addLink(s, s2, g.getLink(s, s2));
			}
		}
	}

	public double getLink(String a, String b) {
		if(g.getEdge(a, b) == null) {
			return 0;
		}
		return g.getEdgeWeight(g.getEdge(a, b));
	}

	public Vector<String> getNodes() {
		return new Vector<String>(g.vertexSet());
	}
	
	public String getRandomNode() {
		return getNodes().get((int) (Math.random() * getNodes().size()));
	}

	public void addLink(String a, String b) {
		addLink(a, b, 1);
	}

	public void addSymLink(String a, String b) {
		addLink(a, b, 1);
		addLink(b, a, 1);
	}

	
	public void addLink(String a, String b, double d) {
		if(d != 0) {
			g.addVertex(a);
			g.addVertex(b);
			g.addEdge(a, b);
			g.setEdgeWeight(g.getEdge(a, b), d);
		}
	}
	
	public void addNode(String a) {
		g.addVertex(a);
	}


	public Vector<String> getNbrs(String node) {
		Set<DefaultWeightedEdge> s = g.outgoingEdgesOf(node);
		Vector<String> edges = new Vector<String>();
		for(DefaultWeightedEdge e : s) {
			edges.add(g.getEdgeTarget(e));
		}
		return edges;
	}

	public int size() {
		return g.vertexSet().size();
	}

	public void incrementLink(String a, String b) {
		if(getEdge(a, b) != 0) {
			g.setEdgeWeight(g.getEdge(a, b), g.getEdgeWeight(g.getEdge(a, b)) + 1);
		}
		else {
			addLink(a, b);
		}
	}
	
	public double getEdge(String a, String b) {
		if(g.getEdge(a, b) != null)
			return g.getEdgeWeight(g.getEdge(a, b));
		else
			return 0;
	}
	
	public StaticEdgeIterator iterator() {
		return new StaticEdgeIterator(this);
	}
	
	public String toString() {
		Vector<String> nodes = getNodes();
		String s = "****************";
		s += "\n";
		for(int i = 0; i < nodes.size(); i++) {
			for(int j = 0; j < nodes.size(); j++) {
				s += getEdge(nodes.get(i), nodes.get(j)) + "\t";
			}
			s += "\n";
		}
		return s;
	}
}
